<template>
  <div>
    <el-row :gutter="10">
      <el-col :span="12">
        <el-card>
          <div id="line" style="width: 100%;height: 400px"/>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <div id="bar" style="width: 100%;height: 400px"/>
        </el-card>
      </el-col>
    </el-row>

    <el-row style="margin: 10px 0px" :gutter="10">
      <el-col span="12">
        <el-card>
          <div id="pie" style="width: 100%;height: 400px"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts';

const option1 = {
  title: {
    text: '销售库存对比表',
    left: 'center'
  },
  legend: {
    left: 'left'
  },
  tooltip: {
    trigger: 'axis'
  },
  xAxis: {
    type: 'category',
    data: []
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name:'销售额',
      data: [],
      type: 'line'
    }
  ]
};

const option2 = {
  title: {
    text: '销售库存对比表',
    left: 'center'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    left: 'left'
  },
  xAxis: {
    type: 'category',
    data: []
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name:'销售额',
      data: [],
      type: 'bar'
    },
  ]
};

const option3 = {
  title: {
    text: 'Referer of a Website',
    subtext: 'Fake Data',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: 'Access From',
      type: 'pie',
      radius: '50%',
      center:['50%','60%'],
      data: [],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
};
export default {
  name: "Charts",
  mounted() {
    let lineDom = document.getElementById('line');
    let lineChart = echarts.init(lineDom);

    let barDom = document.getElementById('bar');
    let barChart = echarts.init(barDom);

    let pieDom = document.getElementById('pie');
    let pieChart = echarts.init(pieDom);


    this.$request.get("/charts").then(res=>{
      //拆线图
      option1.xAxis.data = res.data?.line?.map(v=>v.date) || []
      option1.series[0].data= res.data?.line?.map(v=>v.value) || []
      option1 && lineChart.setOption(option1);

      //柱状图
      option2.xAxis.data = res.data?.bar?.map(v=>v.name) || []
      option2.series[0].data= res.data?.bar?.map(v=>v.value) || []
      option2 && barChart.setOption(option2);

      //饼图
      option3.series[0].data=res.data?.bar || []
      option3 && pieChart.setOption(option3);

    })
  }
}
</script>

<style scoped>

</style>